My journey of C has begun [7]

  • 2020-04-01 21:27:33
  • OfStack

17. Basic data type: Boolean (_Bool type)

_Bool Type is added by C99 to represent a Boolean value, that is, to represent Logic is ( True) and Logical false ( False). Because C represents true with 1 and false with 0, _Bool is really an integer type. In theory, _Bool You only need 1 bit of storage, because 1 bit is enough to represent 0 and 1. In fact, _Bool It is an unsigned integer, typically occupying 1 byte. Such as:   _Bool flag = 1; Flag = 0;   Contains standard header files Stdbool. H After that, we can replace _Bool with bool, true for 1, and false for 0. Such as:   Bool flag = true; Flag = false;   This is done to be compatible with C++.   Pay attention to : Stdbool. H It was added by C99.

18. Basic data type: floating point type

1. The float, double, And long double   The data types mentioned above can only be used to handle integers. If we want to use decimals, we do Floating point types ( Floating - point ). C provides three floating point types: float . Double and Long double . .   C standard requirements float Type at least It's going to be exactly 6 decimal places , and the integer part of the representation must at least reach the range 10-37 -- 10 + 37 . float The general is 32 A.   C standard provision A double The smallest representation range for the integer part of a type is the same as for float, which is & PI; 10-37 to 10 + 37 , but it requires that the decimal part of a double be at least accurate to the decimal point 10 position A double is usually 64-bit.         C also provides Long double Type. The purpose is to provide a more accurate type than double. However, the C standard simply states that a long double must be at least as exact as a double.     2. Declare floating point variables         Floating-point variables are declared and initialized the same way as integer variables. Such as:   Float f_1, f_2; Double d_1; Float f_3 = 6.63; Long double ld_1;     3. Floating point constant   Floating point constants can be written in several ways. The basic form is: write first The integer part (can be signed), and then write The decimal part And then write e or E And then one last one Signed integer . Such as:   + 1.2 e+5
1.5 e-9
5.0 e10   Where e or e is called Exponent sign , e or the signed integer after e is called exponent . Exponent representing Ten to the code power . For example, +1.2E+5 is 1.2 * 10 5 . Let's say that A is the front part of e, and N is the back part of e, then AeN is equal to A * 10 N . In addition, A plus sign You can omit it. The decimal part It's not required, which is to say, 5 e3 It's also true. Order code symbol and order code can also not write, such as: 13.5 . Behind the decimal point, the part of the integer before the order symbol can not be written (9.e5), the integer before the decimal point can not be written ( . E 96-8 ), but not at the same time. Such as:   56.
. 5
3.14
3 the e6
6-8 e   Note: Floating point constants No Spaces! Such as:   3.21 e - 12         / *   There is a space, wrong! * / 3.14 e5             / *   There is a space, wrong! * /   Floating point constants by default A double Type. Suppose var_f is a variable of type float, if you have the following statement:   Var_f = 9.0 * 3.0;   So both 9.0 and 3.0 are constants of type double. Their product is also double. During the assignment, the product is converted to a float and then assigned to var_f.         Of course, we can also specify the type of floating point constant. After the floating point constant f or F , the compiler will use it float Type to handle this constant. Such as: 1.5 f . 2.1 the e6 F . Add it at the back l or L If so, the compiler will use it Long double Type to handle this constant. Such as: 4.1 l . 50.2 e5 L . It's better to capitalize L Because lowercase l It's easy to confuse with the number one.         C99 adds a format for floating-point constants: using hexadecimal prefixes (0x or 0x, 0 is 0 Not the letter o) p or P Instead of e or e, the code stands for Two to the code power . Such as:   0 xb. 1 ep5   Among them b Is equal to 11 in decimal, E is equal to 1 1/16 add 14/256 , p5 is equal to the 2 5 So that's 512. This floating point constant converted to decimal is: 11 plus 1/16 plus 14/256 times 2 5 = 5692   Note: Not all compilers support the new C99 format!     4. Output floating point         Format qualifier % f The command printf outputs float and double floating-point Numbers in decimal form; % e The command printf outputs float and double floating-point Numbers in exponential form. % a. or % a. The command printf function is output in the hexadecimal format that C99 has added, but not all compilers support it. If you want to output Long double Floating point number of type, please use % Lf . % Le . % La Or, % LA . Such as:   /* showfloat. C the display float is used to represent floating point Numbers in two forms   # include < stdio.h >   Int main (void) {       Float var_f = 5.0;       Double var_df e2 = 3.14;       Long double var_ld = 6.51e-5;         Printf ("%f is equal to %e\n", var_f, var_f);       Printf ("%f is equal to %e\n", var_df, var_df);       Printf ("%Lf is equal to %Le\n", var_ld, var_ld);         Return 0; }   The output is as follows:   5.000000 is equal to 5.000000e+00 314.000000 is equal to 3.140000e+02 0.000065 is equal to 6.510000e-05& somalia;   Note: This is the output I got when I compiled and ran GCC 4.02 under Suse Linux 10. If you compile and run this program using dev-c ++ 4.9.9.2, you cannot output var_ld normally. Presumably because the compiler GCC that dev-c ++ USES is 96-bit, it USES the printf function in the library and treats the long double as 64-bit.     5. Overflow and Underflow of floating point Numbers         Suppose your compiler has a maximum float of 3.4e38 if you have the following statement:   Float toobig = 3.4E38 * 100.0f; Printf (" % e \ n ", toobig);   This inevitably leads to overflows! Because toobig can't represent the product of 3.4E38 and 100.0f. The consequences of overflows were not defined in the past, but now C states that if overflows occur, a representation is generated infinity Special value of. Therefore, the value of toobig will eventually become a representation infinity Special value of. In turn, the printf function prints words like inf or infinity.         Divide a floating-point number with a very small absolute value and cause the floating-point number to lose its precision underflow . For example, if 3.1415e-10 is divided by 10, it becomes 0.3141e-10, which is underflow.


Related articles: